using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace NL.UI
{
    public class WinLoading : Window
    {
        public string ChapterName = "";
        public Text ui_ChapterName;
        public Text ui_Tip;
        public float Duration=0.3f;
        public float SizeBig = 30;
        public string SizeColor = "white";
        string _FixedText;
        protected override void Awake()
        {
            base.Awake();
            _FixedText = ui_Tip!=null? ui_Tip.text:"LOADING";
        }

        protected override void OnShow()
        {
            base.OnShow();

            if (ui_ChapterName != null&& !string.IsNullOrEmpty(ChapterName))
                ui_ChapterName.text = ChapterName;
           if (Random.value>0.5f) 
            Invoke("_AddiT", Duration);
           else
            Invoke("_ShineT", Duration);
        }
        int _Pos=0;
        void _AddiT()
        {
            if (ui_Tip == null) return;
            if (ui_Tip.text.Length > _FixedText.Length+6)
                ui_Tip.text = _FixedText;
            else
                ui_Tip.text += ".";

            Invoke("_AddiT", Duration);
        }
        void _ShineT()
        {
            if (ui_Tip == null) return;
            if (++_Pos >= _FixedText.Length) _Pos = 0;

            string _text = "";
            for(int n=0;n<_FixedText.Length;n++)
            {
                if (n == _Pos)
                    _text += $"<color={SizeColor}><size={SizeBig}>{_FixedText[n]}</size></color>";
                else
                    _text += _FixedText[n].ToString();
            }

           // string _char = _FixedText[_Pos].ToString();
           // _char = $"<color=white>{_char}</color>";
            //string _text = _FixedText.Remove(_Pos).Insert(_Pos, _char);//.Replace(_char, $"<color=white>{_char}</color>");
            ui_Tip.text = _text;

            Invoke("_ShineT", Duration);
        }
        public WinLoading SetChapterName(string _name)
        {
            ChapterName = _name;return this;
        }
    }

}
